/* ========================================================================= */
/* ------------------------------------------------------------------------- */
/*!
  \file			pgsqs.cc
  \date			Feb 2012
  \author		TNick

  \brief		Contains the implementation of PgsQs class


*//*


 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 Please read COPYING and README files in root folder
 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*/
/* ------------------------------------------------------------------------- */
/* ========================================================================= */
//
//
//
//
/*  INCLUDES    ------------------------------------------------------------ */

#include	<QDebug>
#include	<QColor>

#include	"pgsqs.h"


/*  INCLUDES    ============================================================ */
//
//
//
//
/*  DEFINITIONS    --------------------------------------------------------- */

using namespace PgSave;

/*  DEFINITIONS    ========================================================= */
//
//
//
//
/*  DATA    ---------------------------------------------------------------- */

/*  DATA    ================================================================ */
//
//
//
//
/*  CLASS    --------------------------------------------------------------- */




/* ------------------------------------------------------------------------- */
PgsQs *		PgsQs::create			( QString s_path )
{
	PgsQs * p_inst_qs = new PgsQs();
	p_inst_qs->setPath( s_path );
	return p_inst_qs;
}
/* ========================================================================= */


/* ------------------------------------------------------------------------- */
PgsQs *		PgsQs::create			(
		QString s_path, QMap<QString, KB_BLIND_LOAD> & other_map )
{
	PgsQs * p_inst_qs = new PgsQs();
	p_inst_qs->setPath( s_path );
	p_inst_qs->uniteKb( other_map );
	return p_inst_qs;
}
/* ========================================================================= */




/* ------------------------------------------------------------------------- */
PgsQs::PgsQs	( void )
#ifdef	USE_PGSAVE_INTERFACE
	:	IPgSave()
	#endif
{
	/* stub */
}
/* ========================================================================= */

/* ------------------------------------------------------------------------- */
PgsQs::~PgsQs	( void )
{
	/* stub */
}
/* ========================================================================= */

/* ------------------------------------------------------------------------- */
bool		PgsQs::setPath			( const QString new_crt )
{
	QString rel_path;
	if ( new_crt.startsWith("/") )
	{
		rel_path = new_crt;
	}
	else
	{
		rel_path  = "/" + stgs_.group() + "/" + new_crt;
	}

	if ( stgs_.group().length() > 0 )
	{
		stgs_.endGroup();
	}

	if ( rel_path != "/" )
		stgs_.beginGroup( rel_path );

	return true;
}
/* ========================================================================= */

/* ------------------------------------------------------------------------- */
QString		PgsQs::path			( void )
{
	QString rel_path = "/" + stgs_.group();
	if ( ! rel_path.startsWith( "/" ) )
		rel_path.prepend( "/" );
	return rel_path;
}
/* ========================================================================= */

/* ------------------------------------------------------------------------- */
void		PgsQs::path			( QStringList & sl_out )
{
	QString rel_path = "/" + stgs_.group();
	sl_out = rel_path.split( "/", QString::SkipEmptyParts );
}
/* ========================================================================= */

/* ------------------------------------------------------------------------- */
void		PgsQs::cdUp			( unsigned steps )
{
	QString rel_path = "/" + stgs_.group();
	if ( ! rel_path.startsWith( "/" ) )
		rel_path.prepend( "/" );
	QStringList sl_out = rel_path.split( "/", QString::SkipEmptyParts );

	if ( sl_out.count() <= (int)steps )
	{
		if ( stgs_.group().length() > 0 )
		{
			stgs_.endGroup();
		}
		return;
	}

	int len = 0;
	for ( int i = 0; i < ( sl_out.count() - (int)steps ); i++ )
	{
		len += sl_out.at(i).length() + 1;
	}

	if ( stgs_.group().length() > 0 )
	{
		stgs_.endGroup();
	}
	stgs_.beginGroup( rel_path.mid( 0, len ) );
	return;

}
/* ========================================================================= */

/* ------------------------------------------------------------------------- */
bool		PgsQs::exists		( QString s_path )
{
	bool b_ret;
	if ( s_path == "/" ) return false;
	if ( s_path.startsWith( "/" ) )
	{ /* absolute path */
		QString pth = stgs_.group();
		if ( pth.length() > 0 )
		{
			stgs_.endGroup();
			b_ret = stgs_.contains( s_path );
			stgs_.beginGroup( pth );
			return b_ret;
		}
		else
		{
			return stgs_.contains( s_path );
		}
	}
	else
	{ /* relative path */
		return stgs_.contains( s_path );
	}

	return false;
}
/* ========================================================================= */

/* ------------------------------------------------------------------------- */
void		PgsQs::blindLoad	( KB_BLIND_LOAD kb_default, void * kb_data )
{

	QStringList sl = stgs_.childGroups();
	for ( int i = 0; i < sl.count(); i++ )
	{
		QString s_ty = stgs_.value( sl.at( i ) + TAG_NAME_TYPE ).toString();
		if ( s_ty.isEmpty() )
			continue;
		if ( ! stgs_.contains( sl.at( i ) + TAG_NAME_VALUE ) )
			continue;
		KB_BLIND_LOAD kb = getKb( s_ty );
		if ( kb == NULL )
		{
			if ( kb_default != NULL )
				kb_default( this, sl.at( i ), kb_data );
			continue;
		}
		kb( this, sl.at( i ), kb_data );
	}
}
/* ========================================================================= */

/* ------------------------------------------------------------------------- */
void		PgsQs::blindLoad	(
		QStringList load_list, KB_BLIND_LOAD kb_default, void * kb_data )
{

	for ( int i = 0; i < load_list.count(); i++ )
	{
		QString s_ty = stgs_.value( load_list.at( i ) + TAG_NAME_TYPE ).toString();
		if ( s_ty.isEmpty() )
			continue;
		if ( ! stgs_.contains( load_list.at( i ) + TAG_NAME_VALUE ) )
			continue;
		KB_BLIND_LOAD kb = getKb( s_ty );
		if ( kb == NULL )
		{
			if ( kb_default != NULL )
				kb_default( this, load_list.at( i ), kb_data );
			continue;
		}
		kb( this, load_list.at( i ), kb_data );
	}
}
/* ========================================================================= */

/* ------------------------------------------------------------------------- */
bool				PgsQs::getBool		( QString s_name, bool * b_ok )
{
	*b_ok = true;
	return stgs_.value( s_name ).toBool();
}
/* ========================================================================= */

/* ------------------------------------------------------------------------- */
qint16				PgsQs::getShort		( QString s_name, bool * b_ok )
{
	return (qint16)stgs_.value( s_name ).toInt( b_ok );
}
/* ========================================================================= */

/* ------------------------------------------------------------------------- */
qint32				PgsQs::getInt		( QString s_name, bool * b_ok )
{
	return (qint32)stgs_.value( s_name ).toInt( b_ok );
}
/* ========================================================================= */

/* ------------------------------------------------------------------------- */
qint64				PgsQs::getLong		( QString s_name, bool * b_ok )
{
	return stgs_.value( s_name ).toLongLong( b_ok );
}
/* ========================================================================= */

/* ------------------------------------------------------------------------- */
qint8				PgsQs::getByte		( QString s_name, bool * b_ok )
{
	return (qint8)stgs_.value( s_name ).toInt( b_ok );
}
/* ========================================================================= */

/* ------------------------------------------------------------------------- */
QColor				PgsQs::getColor		( QString s_name, bool * b_ok )
{
	QColor	col;
	col.setNamedColor( stgs_.value( s_name ).toString() );
	return col;
}
/* ========================================================================= */

/* ------------------------------------------------------------------------- */
qreal				PgsQs::getDouble	( QString s_name, bool * b_ok )
{
	return stgs_.value( s_name ).toDouble( b_ok );
}
/* ========================================================================= */

/* ------------------------------------------------------------------------- */
QStringList			PgsQs::getStrLst	( QString s_name, bool * b_ok )
{
	*b_ok = true;
	return stgs_.value( s_name ).toStringList();
}
/* ========================================================================= */

/* ------------------------------------------------------------------------- */
QString				PgsQs::getStr		( QString s_name, bool * b_ok )
{
	*b_ok = true;
	return stgs_.value( s_name ).toString();
}
/* ========================================================================= */


/* ------------------------------------------------------------------------- */
bool				PgsQs::set		( QString s_name, const qint64 new_val )
{
	stgs_.setValue( s_name, new_val );
	return true;
}
/* ========================================================================= */

/* ------------------------------------------------------------------------- */
bool				PgsQs::set		( QString s_name, const int new_val )
{
	stgs_.setValue( s_name, new_val );
	return true;
}
/* ========================================================================= */

/* ------------------------------------------------------------------------- */
bool				PgsQs::set		( QString s_name, const bool new_val )
{
	stgs_.setValue( s_name, new_val );
	return true;
}
/* ========================================================================= */

/* ------------------------------------------------------------------------- */
bool				PgsQs::set		( QString s_name, const char new_val )
{
	stgs_.setValue( s_name, new_val );
	return true;
}
/* ========================================================================= */

/* ------------------------------------------------------------------------- */
bool				PgsQs::set		( QString s_name, const QRgb new_val )
{
	stgs_.setValue( s_name, new_val );
	return true;
}
/* ========================================================================= */

/* ------------------------------------------------------------------------- */
bool				PgsQs::set		( QString s_name, const QColor new_val )
{
	stgs_.setValue( s_name, new_val.name() );
	return true;
}
/* ========================================================================= */

/* ------------------------------------------------------------------------- */
bool				PgsQs::set		( QString s_name, const QString new_val )
{
	stgs_.setValue( s_name, new_val );
	return true;
}
/* ========================================================================= */

/* ------------------------------------------------------------------------- */
bool				PgsQs::set		( QString s_name, const char * new_val )
{
	stgs_.setValue( s_name, new_val );
	return true;
}
/* ========================================================================= */

/* ------------------------------------------------------------------------- */
bool				PgsQs::set		( QString s_name, const qreal new_val )
{
	stgs_.setValue( s_name, new_val );
	return true;
}
/* ========================================================================= */






#ifdef	PGSCRIPT_TESTS


/* ------------------------------------------------------------------------- */
void				PgSave::PgsQs_Tests					(
		int & performed, int & succeded )
{
	//QString Outcm;
	//bool	b_ok;
	performed = 0;
	succeded = 0;

	qDebug() << "Now testing IPgSave with QSettings implementation";

	/* ------------------------ */
	PgsQs * inst_qs = PgsQs::create( "/" );
	IPgSave_Test_i( inst_qs, performed, succeded );


	delete inst_qs;


	/* ------------------------ */
	inst_qs = PgsQs::create( "/" );
	IPgSave_Test_j( inst_qs, performed, succeded );


	delete inst_qs;

	return;

}
/* ========================================================================= */
#endif

/*  CLASS    =============================================================== */
//
//
//
//
/* ------------------------------------------------------------------------- */
/* ========================================================================= */
